package com.example.app1;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContract;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.app1.Util.DateUtil;

public class RequestActivity extends AppCompatActivity implements View.OnClickListener {
    String sendStr="怀明，出来走走";
//    private ActivityResultLauncher<Intent> register;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_request);
        TextView sendTv = findViewById(R.id.tv_send);
        sendTv.setText(sendStr);
        findViewById(R.id.sendBtn).setOnClickListener(this);
//        register = registerForActivityResult(new StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
//            @Override
//            public void onActivityResult(ActivityResult result) {
//                if(result!=null)
//                {
//                    Intent intent = result.getData();
//                    if(intent!=null&&result.getResultCode()==RESULT_OK)
//                    {
//                        Bundle bundle = intent.getExtras();
//                        String respondTime = bundle.getString("respondTime");
//                        String response = bundle.getString("response");
//                        TextView tv_result = (TextView) findViewById(R.id.tv_result);
//                        tv_result.setText(String.format("时间：%s\n收到的消息是：%s", respondTime, response));
//                    }
//                }
//            }
//        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
                        if(intent!=null&&requestCode==0&&resultCode==RESULT_OK)
                {
                        Bundle bundle = intent.getExtras();
                        String respondTime = bundle.getString("respondTime");
                        String response = bundle.getString("response");
                        TextView tv_result = (TextView) findViewById(R.id.tv_result);
                        tv_result.setText(String.format("时间：%s\n收到的消息是：%s", respondTime, response));
                }
    }

    @Override
    public void onClick(View v) {
        Intent intent = new Intent(this,ResponseActivity.class);
        Bundle bundle=new Bundle();
        bundle.putString("sendtime", DateUtil.getTimeStr());
        bundle.putString("request",sendStr);
        intent.putExtras(bundle);
//        register.launch(intent);
        startActivityForResult(intent,0);
    }
}
